<?php

/**
 * Lumad CMS
 * Copyright (c)	2007, Jason A. Banico
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright (c) 2007, Jason A. Banico
 * @link			http://cakeforge.org/projects/lumad-cms/
 * @package			lumad-cms
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class MenuItemsController extends AppController {

  var $name = 'MenuItems';
  var $components = array('context');
  var $helpers = array('Html','Ajax','Javascript');

  function admin_index() {
    $this->context->method='index';
    $this->context->checkSession($this);
    $this->layout="admin";

    $this->set('menu_items', $this->MenuItem->findAll());
  }

  function admin_swap($from, $to) {
    $this->context->method='index';
    $this->context->checkSession($this);

    // Store values of the menu item records to a temporary holder.
    $fromMenuItem = $this->MenuItem->findById($from);
    $toMenuItem = $this->MenuItem->findById($to);

    // Assign all attributes of the other menu item, except the ID and level.
    $this->data = $fromMenuItem;
    $this->data['MenuItem']['id'] = $toMenuItem['MenuItem']['id'];
    $this->data['MenuItem']['level'] = $toMenuItem['MenuItem']['level'];
    $this->MenuItem->save($this->data['MenuItem']);

    $this->data = $toMenuItem;
    $this->data['MenuItem']['id'] = $fromMenuItem['MenuItem']['id'];
    $this->data['MenuItem']['level'] = $fromMenuItem['MenuItem']['level'];
    $this->MenuItem->save($this->data['MenuItem']); 
    
    // Use a blank layout since this is called via AJAX.
    $this->layout="blank";
    $this->set('menu_items', $this->MenuItem->findAll());
  }

  function admin_activate($id) {
    $this->context->method='index';
    $this->context->checkSession($this);

    $this->data = $this->MenuItem->findById($id);
    $this->data['MenuItem']['published']='1';
    $this->MenuItem->save($this->data['MenuItem']); 

    $this->layout="blank";
    $this->set('menu_items', $this->MenuItem->findAll());
  }
    
  function admin_deactivate($id) {
    $this->context->method='index';
    $this->context->checkSession($this);

    $this->data = $this->MenuItem->findById($id);
    $this->data['MenuItem']['published']='0';
    $this->MenuItem->save($this->data['MenuItem']); 

    $this->layout="blank";
    $this->set('menu_items', $this->MenuItem->findAll());
  }

  function admin_levelin($id) {
    $this->context->method='index';
    $this->context->checkSession($this);

    $this->data = $this->MenuItem->findById($id);
    $this->data['MenuItem']['level']+=1;
    $this->MenuItem->save($this->data['MenuItem']); 

    $this->layout="blank";
    $this->set('menu_items', $this->MenuItem->findAll());
  }

  function admin_levelout($id) {
    $this->context->method='index';
    $this->context->checkSession($this);

    $this->data = $this->MenuItem->findById($id);
    $this->data['MenuItem']['level']-=1;
    $this->MenuItem->save($this->data['MenuItem']); 

    $this->layout="blank";
    $this->set('menu_items', $this->MenuItem->findAll());
  }

  function getMenuItems() {
    return $this->MenuItem->findAll('published=1');
  }

  function admin_add() {
    $this->context->method='add';
    $this->context->checkSession($this);
    $this->layout="admin";

    if (!empty($this->data)) {
      if ($this->MenuItem->save($this->data)) {
        $this->redirect('/admin/menu_items/');
      }
    }
  }

  function admin_edit($id = null) {
    $this->context->checkSession($this);
    $this->layout="admin";

    if (empty($this->data)) {
      $this->MenuItem->id = $id;
      $this->data = $this->MenuItem->read();
      $this->set('menuitem', $this->MenuItem->read());
    }
    else
    {
      $this->MenuItem->save($this->data['MenuItem']);
      $this->redirect('/admin/menu_items/');
    }
  }

  function admin_delete($id = null) {
    $this->context->checkSession($this);
    $this->layout="admin";

    if (empty($this->data)) {
      $this->MenuItem->id = $id;
      $this->data = $this->MenuItem->read();
      $this->set('menuitem', $this->MenuItem->read());
    }
    else
    {
      $this->MenuItem->del($this->data['MenuItem']['id']);
      $this->redirect('/admin/menu_items/');
    }
  }

}
?>